//
//  VETFlashTableViewCell.m
//  VEMoneyPorject
//
//  Created by BlueWind on 2018/9/17.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "VETFlashTableViewCell.h"

@implementation VETFlashTableViewCell

{
    UIButton *_timeButton;
    
    UILabel *_titleLabel;
    
    UILabel *_summaryLabel;
    
    RankButton *_shareButton;
    
    RankButton *_haveButton;
}

/** 返回循环利用的cell */
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self= [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self loadDefaultSetting];
    }
    return self;
}

/** 加载默认UI元素和需要的数据 */
- (void)loadDefaultSetting {
    
    UIView *line = [UIView new];
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(GAP15);
        make.size.mas_equalTo(CGSizeMake(1, 20));
    }];
    line.backgroundColor = [UIColor colorWithHexString:kLineViewBgColor];
    
    UIView *redYuan = [UIView new];
    [self.contentView addSubview:redYuan];
    [redYuan mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom);
        make.centerX.equalTo(line);
        make.size.mas_equalTo(CGSizeMake(8, 8));
    }];
    [redYuan setLayerCornerRadius:4];
    redYuan.backgroundColor = [UIColor redColor];
    
    UIView *line1 = [UIView new];
    [self.contentView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(redYuan.mas_bottom);
        make.centerX.equalTo(redYuan);
        make.bottom.equalTo(self.contentView);
        make.width.mas_equalTo(1);
    }];
    line1.backgroundColor = [UIColor colorWithHexString:kLineViewBgColor];

    _timeButton = [UIButton new];
    [self.contentView addSubview:_timeButton];
    [_timeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(redYuan);
        make.left.equalTo(redYuan.mas_right).offset(GAP15);
    }];
    _timeButton.contentEdgeInsets = UIEdgeInsetsMake(3, 5, 3, 5);
    _timeButton.backgroundColor = [UIColor colorWithHexString:kDetailTextGrayBlackColor];
    [_timeButton setTitle:@"12:12" forState:(UIControlStateNormal)];
    _timeButton.userInteractionEnabled = NO;
    _timeButton.titleLabel.font = kFont(12);
    _timeButton.layerCornerRadius = 6;
    _titleLabel = [UILabel new];
    [self.contentView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_timeButton.mas_bottom).offset(GAP15);
        make.left.equalTo(self->_timeButton);
        make.right.equalTo(self.contentView).offset(-GAP15);
    }];
    _titleLabel.font = kBoldFont(18);
    
    _titleLabel.text = @"新品发布会上首次介绍";
    
    _summaryLabel = [UILabel new];
    [self.contentView addSubview:_summaryLabel];
    [_summaryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_titleLabel.mas_bottom).offset(GAP15);
        make.left.equalTo(self->_titleLabel);
        make.right.equalTo(self->_titleLabel);
    }];
    _summaryLabel.font = kFont(15);
    _summaryLabel.text = @"苹果是在去年9月的秋季新品发布会上首次介绍了AirPower，可是1年的时间过去了，我们对这款产品的期待却仅仅停留在当时那个模糊的承诺“2018年上市”";
    _summaryLabel.numberOfLines = 4;
    
    _shareButton = [RankButton buttonWithType:(UIButtonTypeCustom)];
    [self.contentView addSubview:_shareButton];
    [_shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_summaryLabel.mas_bottom).offset(GAP15);
        make.bottom.equalTo(self.contentView).offset(-GAP15);
        make.right.equalTo(self.contentView).offset(-GAP15);
        make.height.mas_equalTo(26);
    }];
    _shareButton.picTileRange = 4;
    [_shareButton setImage:[UIImage imageNamed:@"挖矿"] forState:(UIControlStateNormal)];
    _shareButton.titleLabel.font = kFont(13);
    [_shareButton setTitle:@"分享挖矿" forState:(UIControlStateNormal)];
    _shareButton.contentEdgeInsets = UIEdgeInsetsMake(1, GAP8, 1, GAP8);
    [_shareButton setLayerCornerRadius:13];
    [_shareButton layerBorderWithWidth:0.5 borderColor:[UIColor redColor]];
    [_shareButton addTarget:self action:@selector(shareNews) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    _haveButton = [RankButton buttonWithType:(UIButtonTypeCustom)];
    _haveButton.type = buttonTypePicRight;
    [self.contentView addSubview:_haveButton];
    [_haveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self->_shareButton);
        make.right.equalTo(self->_shareButton.mas_left).offset(-GAP15);
        make.height.mas_equalTo(26);
    }];
    _haveButton.picTileRange = 4;
    [_haveButton setImage:[UIImage imageNamed:@"币子"] forState:(UIControlStateNormal)];
    [_haveButton setTitle:@"剩余1234" forState:(UIControlStateNormal)];
    _haveButton.contentEdgeInsets = UIEdgeInsetsMake(1, GAP8, 1, GAP8);

}

- (void)setFastModel:(VEFastModel *)fastModel {
    _fastModel = fastModel;
    _titleLabel.text = fastModel.ARTICLETITLE;
    
    
    NSMutableAttributedString *attrStr =  [[NSMutableAttributedString alloc] initWithData:[fastModel.CONTENT dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    [attrStr setAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:15.0],NSForegroundColorAttributeName:[UIColor colorWithHexString:kDetailTextBlackColor]} range:NSMakeRange(0, attrStr.length)];

    _summaryLabel.attributedText = attrStr;
    
    CGFloat height =  [_summaryLabel.attributedText boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 30, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;//针对富文本有专门的计算动态高度的方法，与nsstring 大同小异；
    
    if (height > 100) {
        _summaryLabel.numberOfLines = 0;
    } 
    
    _summaryLabel.height = height;
    
    [_haveButton setTitle:[NSString stringWithFormat:@"剩余%ld",(long)fastModel.COINNUMBER] forState:(UIControlStateNormal)];

    NSString *timeStr = [NSDate dateDescriptionWithTargetDate:fastModel.RELEASETIME andTargetDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    
    
    [_timeButton setTitle:timeStr forState:(UIControlStateNormal)];
    
}

- (CGFloat)getHeightWihtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat height =  [_summaryLabel.attributedText boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 30, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
    if (self.fastModel.isSpread == NO) {
        
        if (height > 92) {
            return 240;
        } else {
            return height + 148;
        }

    } else {
        return 148 + height;
    }
}


- (NSAttributedString *)attributedStringWithHTMLString:(NSString *)htmlString

{
    
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
                               
                               NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };
    
    NSData *data = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
    
    return [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:nil];
    
}

- (void)shareNews {
    if (_shareFast) {
        _shareFast();
    }
}


@end
